<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class PublicController extends Controller {

      protected function _initialize(){
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
    }

     public function login()
    {
        //获取登录信息
        if (IS_POST) {
            $username=I('post.username');
            $password=I('post.password');
            $res = D('Agent')->login($username, $password);
            if(0 < $res){ //UC登录成功
                $this->success('登录成功！', U('Index/index'));
            } else { //登录失败
                switch ($res) {
                    case '-2':
                        $info='用户名或密码不正确';
                        break;
                    case '0':
                        $info='用户正在审核';
                        break;
                    case '2':
                        $info='用户审核失败';
                        break;    
                    default:
                        $info='用户不存在！';
                        break;
                }
               $this->error($info, '', true);
            }

        }else{
            session('user_auth', null);
            session('user_auth_sign', null);
            $this->display();
        }

    }

     //注册
    public function register(){
        //获取登录信息
        if (IS_POST) {
            $data=I('post.');

            $re=M('Agent')->where(array('mobile'=>$data['mobile']))->find();
            if(is_array($re)){
                $this->error('手机已注册','', true);
            }
         
            $uid    =   D('Agent')->register($data);
            if(0 < $uid){ //注册成功
                $this->success('注册成功！',U('login'));
            } else { //注册失败，显示错误信息
                $this->error($this->showRegError($uid),'', true);
            }
        }else{
            $this->display();
        }
    }


    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function updatePassword(){
        if(IS_POST){
            //获取参数
            $data=I('post.');
            $password   =   I('post.old');
            empty($password) && $this->error('请输入原密码');
            empty($data['password']) && $this->error('请输入新密码');

            $repassword = I('post.repassword');
            empty($repassword) && $this->error('请输入确认密码');

            if($data['password'] !== $repassword){
                $this->error('您输入的新密码与确认密码不一致');
            }

            unset($data['old']);
            unset($data['repassword']);

            $res    =   D('Home/Agent')->updateInfo($password, $data);
            if($res['status']){
                session('user_auth', null);
                session('user_auth_sign', null);
                $this->success('修改密码成功！',U('login'));
            }else{
                $this->error($res['info']);
            }
        }else{
            if (!is_login()){
                $this->redirect("Public/login");
            }
            $this->display();
        }

    }


    /* 退出登录 */
    public function logout(){
        if(is_login()){
            session('user_auth', null);
            session('user_auth_sign', null);
            $this->redirect('login');
        } else {
            $this->redirect('login');
        }
    }

    
    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    public function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须时6位的数字加字符格式！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }


}